package com.ylfstu.single_;

public class SingleTon01 {
//    private static int n1;

    public static void main(String[] args) {
//        GirlFriend girlFriend1 = new GirlFriend("小红");
//        GirlFriend girlFriend2 = new GirlFriend("小蓝");
//        GirlFriend instance1 = GirlFriend.getInstance();
//        System.out.println(instance1);
//
//        GirlFriend instance2 = GirlFriend.getInstance();
//        System.out.println(instance2);

//        n1 = 100;
//        System.out.println(n1);
    }
}

class GirlFriend {
    private String name;
    private static GirlFriend girlFriend = new GirlFriend("小青");

    //如何保障只能创建一个GirlFriend对象\
    //单例模式-饿汉式：
    //1.私有化构造器
    //2.在类的内部直接创建对象，
    //3.提供一个公共的static方法，返回girlFriend对象
    private GirlFriend(String name) {
        this.name = name;
    }

    public static GirlFriend getInstance() {
        return girlFriend;
    }

    @Override
    public String toString() {
        return "GirlFriend{" +
                "name='" + name + '\'' +
                '}';
    }
}

class GirlF {
    private String name;
    private static GirlF gf;

    private GirlF(String name) {
        this.name = name;
    }

    public static GirlF getInstance() {
        if (gf == null) {
            gf = new GirlF("小雨");
        }
        return gf;
    }

    @Override
    public String toString() {
        return "GirlF{" +
                "name='" + name + '\'' +
                '}';
    }
}
